# debugtalk.py
import time
import hashlib
import requests
import json
import gzip
import datetime


def get_videosrc():
    data = dict()
    data['cover'] = "http://b00.cdn.ipalfish.com/0/img/b1/0c/750a706f1ae2415cbaeff75daa0b"
    data['md5'] = "77a195f13aa256b0108cb692034ab159"
    data['url'] = "http://b00.cdn.ipalfish.com/0/img/77/a1/95f13aa256b0108cb692034ab159"
    return json.dumps(data)

def get_baseurl(response):
    url = response.url
    global base_url
    base_url = url[:-14]

def get_localid():
    if ('localid' in locals().keys()):
        pass
    else:
        localid = 0

    t = int(round(time.time() * 1000))
    if (t > (localid >> 10)):
        localid = (t << 10)
    else:
        ++localid
    return localid

def get_drawstate_version(version):
    if version > 0:
        return version
    else:
        return 0

#获取时间戳  参数t -  s:秒级时间戳   else: 毫秒级时间戳
def get_now_timestamp(t = 'ms'):
    t = str(t).lower()
    tt = time.time()
    if t == 's' :
        return int(tt)
    else:
        return int(round(tt * 1000))

def get_timestamp_sec():
    homework_timestamp = int(round(time.time()))
    return homework_timestamp

def get_timestamp_minisec():
    homework_timestamp = int(round(time.time()) * 1000)
    return homework_timestamp

def get_time(dt):
    dt = str(dt)
    # 转换成时间数组
    timeArray = time.strptime(dt, "%Y%m%d%H%M%S")
    # 转换成时间戳
    timestamp = time.mktime(timeArray)
    print(int(timestamp))
    return int(timestamp)

def get_pw(pw):
    pw = str(pw)
    m = hashlib.md5()
    b = pw.encode(encoding='utf-8')
    m.update(b)
    pw_md5 = m.hexdigest()
    return pw_md5[:16]

def cancel_time_substar(data_req):
    resp = requests.post(url=data_req['url'], json=data_req['json'])
    print(resp.text)

def reserve_my(data_req):
    resp = requests.post(url=data_req['url'], json=data_req['json'])
    print(resp.text)

    data_resp = json.loads(resp.content)
    classroominfos = data_resp['data']['ent']['classroominfos']

    data_ret = list()
    for classroominfo in classroominfos:
        print(type(classroominfo))
        for key in classroominfo.keys():
            if key == 'stamp':
                print(classroominfo[key])
                data_ret.append(classroominfo[key])

    print(data_ret)
    return data_ret

def reserve_cancel_all(token, cate, uid, url):
    data_reserve_my = dict()
    data_reserve_my['token'] = token
    data_reserve_my['cate'] = cate
    data_reserve_my['h_m'] = uid

    req_reservce_my = dict()
    # req_reservce_my['url'] = 'https://test.ipalfish.com/klian/reserve/my'
    # req_reservce_my['url'] = 'https://m.ipalfish.com/klian/reserve/my'
    req_reservce_my['url'] = url + '/reserve/my'
    req_reservce_my['json'] = data_reserve_my

    stamp_list = reserve_my(req_reservce_my)

    data_cancel_time_substar = dict()
    data_cancel_time_substar['token'] = token
    data_cancel_time_substar['cate'] = cate
    data_cancel_time_substar['h_m'] = uid

    req_cancel_time_substar = dict()
    # req_cancel_time_substar['url'] = 'https://test.ipalfish.com/klian/ugc/curriculum/classroom/cancel/time/substar'
    # req_cancel_time_substar['url'] = 'https://m.ipalfish.com/klian/ugc/curriculum/classroom/cancel/time/substar'
    req_cancel_time_substar['url'] = url + '/ugc/curriculum/classroom/cancel/time/substar'
    req_cancel_time_substar['json'] = data_cancel_time_substar
    if (len(stamp_list) > 0):
        for stamp in stamp_list:
            req_cancel_time_substar['json']['stamp'] = stamp
            cancel_time_substar(req_cancel_time_substar)

def data_gzip(request):
    data_json = json.dumps(request['data'])
    data_bytes = data_json.encode()
    data_gzip = gzip.compress(data_bytes)
    request['data'] = data_gzip

def online_login(phone,pwd):
    url = 'https://m.ipalfish.com/klian/account/login'
    pwd = get_pw(pwd)
    data = dict()
    data['h_lc'] = 'zh-Hans-CN'
    data['h_ch'] = 'InHouse'
    data['h_dt_sub'] = 1
    data['zone'] = 28800
    data['h_src'] = 3
    data['cate'] = 1
    data['phone'] = phone
    data['pw'] = pwd
    data['area'] = '86'
    data['h_ts'] = get_timestamp_minisec()
    data['h_av'] = '4.0.630'
    data['phone'] = phone
    data['hnt'] = 1
    data['h_did'] = '58c0e05a2174e760f8ba2c031d1a9aacb53efb4c'
    data['h_m'] = 25757114
    data['h_bu'] = '20190620.212149'
    data['h_p'] = 211
    data['h_dt'] = 1

    resp = requests.post(url=url,json=data)
    data_resp = json.loads(resp.content)
    return data_resp['data']['token']

#auth认证
def op_login(request):
    url = "https://test.ipalfish.com:30000/auth/login"
    data = dict()
    data['utype'] = "op"
    data['user'] = "liuyuan2680"
    data['ttype'] = "phone"
    data['ticket'] = ""

    resp = requests.post(url=url,json=data)
    request['cookies'] = resp.cookies

def get_hold_time(days):
    now_time = datetime.datetime.now().replace(hour=3,minute=0, second=0,microsecond=0)
    hold_time = now_time + datetime.timedelta(days=days)
    hold_stamp = hold_time.timestamp()
    return int(hold_stamp)